package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface AlbumInfoMapper extends BaseMapper<AlbumInfo> {

    /**
     * 用户专辑分页列表查询
     * @param albumListVoPage
     * @param albumInfoQuery
     * @return
     */
    IPage<AlbumListVo> selectUserAlbumPage(Page<AlbumListVo> albumListVoPage,@Param("vo") AlbumInfoQuery albumInfoQuery);


    /**
     * 专辑统计信息
     * @param albumId
     * @return
     */
    @Select("select\n" +
            "    stat.album_id,\n" +
            "    max(if(stat.stat_type = '0401',stat.stat_num,0)) playStatNum,\n" +
            "    max(if(stat.stat_type = '0402',stat.stat_num,0)) subscribeStatNum,\n" +
            "    max(if(stat.stat_type = '0403',stat.stat_num,0)) buyStatNum,\n" +
            "    max(if(stat.stat_type = '0404',stat.stat_num,0)) commentStatNum\n" +
            "from album_stat stat\n" +
            "where stat.album_id = #{albumId}\n" +
            "group by stat.album_id")
    AlbumStatVo selectAlbumStatVo(@Param("albumId") Long albumId);


    /**
     * 更新专辑播放量
     * @param albumId
     * @param statType
     * @param count
     */
    @Update("update album_stat set stat_num = stat_num + #{count} where album_id = #{albumId} and stat_type = #{statType}")
    void updateAlbumStat(@Param("albumId") Long albumId, @Param("statType") String statType, @Param("count") Integer count);
}
